package com.ruoyi.web.controller.workJob;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.RedisNoUtil;
import com.ruoyi.workFlow.service.OperationalWorkFlowService;
import com.ruoyi.worker.domain.OperationalRequirementsTable;
import com.ruoyi.worker.domain.SchemeConfiguration;
import com.ruoyi.worker.service.OperationalService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OperationController
 * @Description 运营管理控制层
 * @Author leizhang
 * Date 2021/10/12 3:38 下午
 * @Version 1.0
 **/
@RestController
@RequestMapping("/operationalApi")
@Slf4j
public class OperationController extends BaseController {

    @Autowired
    private OperationalService operationalService;
    @Autowired
    private OperationalWorkFlowService operationalWorkFlowService;
    @Resource
    private RedisTemplate redisTemplate;
    /**
     * @param requestParam
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 获取设备/人员配置信息
     * @Author : leizhang
     * @Date 9:59 上午 2021/10/13
     **/
    @GetMapping("/getEquipmentsAndDeps")
    public AjaxResult getEquipmentAndDeps(@RequestParam Map<String, Object> requestParam) {
        Map<String, Object> equipmentAndDeps = operationalService.getEquipmentAndDeps(requestParam);
        return AjaxResult.success(equipmentAndDeps);
    }

    /**
     * @param
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 新增方案配置
     * @Author : leizhang
     * @Date 10:04 上午 2021/10/13
     **/
    @PostMapping("/addSchemeConfiguration")
    public AjaxResult addSchemeConfiguration(@RequestBody Map<String, Object> requestBody) {
        try {
            int result = operationalService.addSchemeConfiguration(requestBody);
            if (1 == result) {
                return AjaxResult.error(requestBody.get("businessClassification").toString().concat("(w)业务量重复"));
            }else if(2 == result){
                return AjaxResult.error(requestBody.get("programmeName").toString().concat("的方案名称已经存在了"));
            }
        } catch (Exception e) {
            log.error("新增方案配置失败信息:{}", e.getMessage());
            return AjaxResult.error("新增方案配置失败");
        }
        return AjaxResult.success();
    }

    /**
     * @param schemeConfiguration
     * @return com.ruoyi.common.core.page.TableDataInfo
     * @Description : 获取配置方案列表信息
     * @Author : leizhang
     * @Date 10:22 上午 2021/10/13
     **/
    @GetMapping("/list")
    public TableDataInfo getSchemeConfigurationTable(SchemeConfiguration schemeConfiguration) {
        startPage();
        List<Map<String, Object>> tableList = operationalService.getSchemeConfigurationTable(schemeConfiguration);
        return getDataTable(tableList);
    }

    /**
     * @param requestBody
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 更新方案配置状态
     * @Author : leizhang
     * @Date 5:59 下午 2021/10/13
     **/
    @PostMapping("/updateSchemeConfiguration")
    public AjaxResult updateSchemeConfiguration(@RequestBody Map<String, Object> requestBody) {
        int result = operationalService.updateSchemeConfiguration(requestBody);
        if (1 == result) {
            return AjaxResult.error(requestBody.get("businessClassification").toString().concat("(w)业务量重复"));
        }else if(2 == result){
            return AjaxResult.error(requestBody.get("programmeName").toString().concat("的方案名称已经存在了"));
        }

        return AjaxResult.success();
    }



    /**
     * @param ids
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 方案配置信息删除
     * @Author : leizhang
     * @Date 1:37 下午 2021/10/18
     **/
    @PostMapping("/delSchemeConfigurationInfo")
    public AjaxResult delSchemeConfigurationInfo(@RequestBody List<Integer> ids) {
        operationalService.delSchemeConfigurationInfo(ids);
        return AjaxResult.success();
    }

    /**
     * @param
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 运营需求单-班组/班次信息查询
     * @Author : leizhang
     * @Date 1:31 下午 2021/10/14
     **/
    @GetMapping("/getTimeShiftAndDep")
    public AjaxResult getTimShiftAndDep(@RequestParam Map<String, Object> requestParam) {
        Map<String, Object> result = operationalService.getTimeShiftAndDep(requestParam);
        return AjaxResult.success(result);
    }

    /**
     * @param
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 获取启用方案/预估业务量数据接口
     * @Author : leizhang
     * @Date 2:27 下午 2021/10/14
     **/
    @GetMapping("/getStrategyValue")
    public AjaxResult getQualityPredictInfo(@RequestParam Map<String, Object> requestParam) {
        Map<String, Object> qualityPredictInfo = operationalService.getQualityPredictInfo(requestParam);
        return AjaxResult.success(qualityPredictInfo);
    }

    /**
     * @param id
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 获取启动的方案信息，已经方案超限的配置信息
     * @Author : leizhang
     * @Date 10:53 上午 2021/10/15
     **/
    @GetMapping("/getSchemeConfigAndStrategy/{id}")
    public AjaxResult getSchemeConfigurationAndStrategy(@PathVariable("id") int id) {
        Map<String, Object> resultMap = operationalService.getSchemeConfigurationAndStrategy(id);
        return AjaxResult.success(resultMap);
    }

    /**
     * @param
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 运营需求单新增
     * @Author : leizhang
     * @Date 2:23 下午 2021/10/15
     **/
    @PostMapping("/addOperational")
    public AjaxResult addOperationalInfo(@RequestBody Map<String, Object> requestParam) {
        int i = operationalService.addOperationalInfo(requestParam);
        if (-1 == i) {
            return AjaxResult.error("单据编号格式不正确");
        }
        if (-2 == i) {
            return AjaxResult.error("单据编号重复");
        }
        return AjaxResult.success();
    }

    /**
     * @param
     * @return com.ruoyi.common.core.page.TableDataInfo
     * @Description : 获取运营需求列表
     * @Author : leizhang
     * @Date 9:13 下午 2021/10/15
     **/
    @GetMapping("/getOperationalInfo")
    public TableDataInfo getOperationalList(@RequestParam Map<String, Object> requestParam) {
        startPage();
        List<Map<String, Object>> tableList = operationalService.getOperationalList(requestParam);
        return getDataTable(tableList);
    }

    /**
     * @param requestParam
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 运营需求详情获取
     * @Author : leizhang
     * @Date 11:14 下午 2021/10/15
     **/
    @GetMapping("/getOperationalDetail")
    public AjaxResult getOperationalDetail(@RequestParam Map<String, Object> requestParam) throws Exception {
        Map<String, Object> operationalDetail = operationalService.getOperationalDetail(requestParam);
        return AjaxResult.success(operationalDetail);
    }

    /**
     * @param
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description :  更新审核状态
     * @Author : leizhang
     * @Date 9:54 下午 2021/10/16
     **/
    @PostMapping("/updateAuditInfo")
    public AjaxResult updateAuditInfo(@RequestBody Map<String, Object> requestParam) {
        operationalService.updateAuditInfo(requestParam);
        return AjaxResult.success();
    }

    /**
     * @param requestParam
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description :运营需求单修改
     * @Author : leizhang
     * @Date 10:14 下午 2021/10/17
     **/
    @PostMapping("/updateOperationalInfo")
    public AjaxResult updateOperationalInfo(@RequestBody Map<String, Object> requestParam) {
        operationalService.updateOperationalInfo(requestParam);
        return AjaxResult.success();
    }

    /**
     * @Description : 运营需求单删除
     * @Author : leizhang
     * @Date 2:34 下午 2021/10/18
     * @param ids
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @PostMapping("/delOperationalInfo")
    public AjaxResult delOperationalInfo(@RequestBody List<Integer> ids) {
        operationalService.delOperationalInfo(ids);
        return AjaxResult.success();
    }

    /**
     *
     * 功能描述:获取班次列表
     * @Date 2021/11/12 13:35
     * @param
     * @param requestParam
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author 刘松山
     */
    @GetMapping("/getTimShiftInfo")
    public TableDataInfo getTimShiftInfo(@RequestParam Map<String, Object> requestParam) {
        List<Map<String, Object>> result = operationalService.getTimShiftInfo(requestParam);
        return getDataTable(result);
    }


    /**
     * 更新用工需求预测需求人数
     * @param requestParam: {id:主键,demandNumber:实际用工人数}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author Hush.
     * @since 2021/11/29 19:19
     */
    @PostMapping("/updateDemandNumber")
    public AjaxResult updateDemandNumber(@RequestBody Map<String, Object> requestParam) {
        operationalService.updateDemandNumber(requestParam);
        return AjaxResult.success();
    }


    /**---------------------运营需求单二期--------------------------------**/
    @GetMapping("/getOrderCode")
    public AjaxResult getOrderCode(){
        String orderCode = RedisNoUtil.getOrderCode("YG", redisTemplate);
        return AjaxResult.success("YG"+orderCode);
    }

    /**
     * @param
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description :获取配置详情
     * @Author : leizhang
     * @Date 9:40 下午 2021/10/13
     **/
    @GetMapping("/getSchemeConfigurationDetail/{id}")
    public AjaxResult getSchemeConfigurationDetail(@PathVariable("id") int id) {
        Map<String, Object> details = operationalService.getSchemeConfigurationDetail(id);
        return AjaxResult.success(details);
    }

    /**
     * @Description : 获取
     * @Author : leizhang
     * @Date 11:19 PM 2021/12/23
     * @param
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @GetMapping("/getFaResult")
    public AjaxResult getFaResult( OperationalRequirementsTable operationalRequirementsTable){
        Map<String, Object> faInfo = operationalService.getFaInfo(operationalRequirementsTable);
        return AjaxResult.success(faInfo);
    }

    /**
     * @Description : 获取排班人数
     * @Author : leizhang
     * @Date 11:35 PM 2021/12/26
     * @param requesParam
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @GetMapping("/getWorkerNumber")
    public AjaxResult getWorkerNumber(@RequestParam Map<String,Object> requesParam){
        try {
            Map<String, Object> workerNumber = operationalService.getWorkerNumber(requesParam);
            return AjaxResult.success(workerNumber);
        } catch (Exception e) {
            return AjaxResult.error("获取排班人数失败");
        }
    }

    /**
     * 获取运营需求(二期)详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(operationalService.selectOperationalRequirementsTableById(id));
    }

    @GetMapping("/operationalList")
    public TableDataInfo list(OperationalRequirementsTable operationalRequirementsTable)
    {
        startPage();
        List<OperationalRequirementsTable> list = operationalService.selectOperationalRequirementsTableList(operationalRequirementsTable);
        return getDataTable(list);
    }

    @Log(title = "运营需求(二期)", businessType = BusinessType.UPDATE)
    @PostMapping("/operationalUpdate")
    public AjaxResult edit(@RequestBody Map<String,Object>  operationalRequirementsTable)
    {
        return toAjax(operationalWorkFlowService.updateOperationalRequirementsTable(operationalRequirementsTable));
    }

    @Log(title = "运营需求(二期)", businessType = BusinessType.INSERT)
    @PostMapping("/operationalAdd")
    public AjaxResult add(@RequestBody OperationalRequirementsTable operationalRequirementsTable) throws Exception
    {
        int i = operationalWorkFlowService.insertOperationalRequirementsTable(operationalRequirementsTable);
        if (-1 == i) {
            String msg = "当天班次已有需求单，不能重复创建!";
            return AjaxResult.error(msg);
        }else {
            return AjaxResult.success();
        }
    }

    @Log(title = "运营需求(二期)", businessType = BusinessType.UPDATE)
    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id)
    {
        return toAjax(operationalService.deleteOperationalRequirementsTableById(id));
    }
    /**
     * 获取运营需求(二期)详细信息
     */
    @GetMapping(value = "/getAttendanceInformation")
    public AjaxResult getAttendanceInformation(@RequestParam Map<String,Object> getAttendanceInformation) throws Exception {
        return AjaxResult.success(operationalService.getAttendanceInformation(getAttendanceInformation));
    }

}
